/*
 * Copyright (c) 2016 www.smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShCompanyExpressDao;
import com.smarthome.base.dao.base.BaseDao;
import com.smarthome.base.service.ShCompanyExpressService;
import com.smarthome.base.service.base.BaseServiceImpl;
import com.smarthome.domain.bean.ShCompanyExpress;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.domain.vo.boss.Tree;

/**
 * ShCompanyExpressService 实现类
 * @author KongXiangyu
 * @since 2016-03-21
 */
@Service("shCompanyExpressService")
public class ShCompanyExpressServiceImpl extends BaseServiceImpl<ShCompanyExpress, Long> implements ShCompanyExpressService {

    @Resource
    private ShCompanyExpressDao shCompanyExpressDao;

    public BaseDao<ShCompanyExpress, Long> getDao() {
        return shCompanyExpressDao;
    }

	@Override
	public DataGrid quetyDataGrid(ShCompanyExpress companyExpress, PageHelper page) {
		DataGrid grid = new DataGrid();

		

		int startIndex = (page.getPage() - 1) * page.getRows();
		int endIndex = page.getPage() * page.getRows();
		Long count = shCompanyExpressDao.queryCompanyExpressCount(companyExpress);
		grid.setTotal(count);

		if (grid.getTotal() > 0 && grid.getTotal() > startIndex) {
			companyExpress.setStartIndex(startIndex);
			companyExpress.setEndIndex(endIndex);
			if (!StringUtils.isBlank(page.getOrder())) {
				companyExpress.setOrderFieldType(page.getOrder());
			}
			if (!StringUtils.isBlank(page.getSort())) {
				companyExpress.setOrderField(page.getSort());
			}
			List<ShCompanyExpress> rows = shCompanyExpressDao.queryCompanyExpress(companyExpress);
			grid.setRows(rows);
		}

		return grid;
	}

	@Override
	public Tree getTree(ShCompanyExpress companyExpress) {
		List<ShCompanyExpress> companyExpressList = this.selectEntryList(companyExpress);
		List<Tree> companyExpressTrees = new ArrayList<Tree>();
		int length = companyExpressList.size();
		Tree companyExpressTree = null;
		ShCompanyExpress express = null;
		for(int i = 0;i < length;i++) {
			express = companyExpressList.get(i);
			companyExpressTree = new Tree(express.getId(), express.getName(), 0l);
			companyExpressTrees.add(companyExpressTree);
		}
		Tree root = new Tree(0l, "快递点");
		root.setChildren(companyExpressTrees);
		return root;
	}
}